package com.badlogic.androidgames.framework.impl;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;
import androidgames.networkgames.cardduel.R;
import androidgames.networkgames.screens.components.CustomKeyboardView;

import com.badlogic.androidgames.framework.Audio;
import com.badlogic.androidgames.framework.FileIO;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;
import com.badlogic.androidgames.framework.Input;
import com.badlogic.androidgames.framework.Screen;

public abstract class AndroidGame extends Activity implements Game {
    AndroidFastRenderView renderView;
    Graphics graphics;
    Audio audio;
    Input input;
    FileIO fileIO;
    Screen screen;
    WakeLock wakeLock;
    // Add start 	Tinh 	23/11/2012
    public static PackageInfo packageInfo = null;
    // Add end 		Tinh 	23/11/2012
    
    // Add start 	Dao 	24/11/2012
    RelativeLayout rootLayout;
    CustomKeyboardView keyboardView;
    // Add end 		Dao 	24/11/2012
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Add start 	Tinh 	23/11/2012
        try {
			packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        // Add end 		Tinh 	23/11/2012
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
        int frameBufferWidth = isLandscape ? 800 : 480;
        int frameBufferHeight = isLandscape ? 480 : 800;
        Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565);
        
        float scaleX = (float) frameBufferWidth / getWindowManager().getDefaultDisplay().getWidth();
        float scaleY = (float) frameBufferHeight / getWindowManager().getDefaultDisplay().getHeight();

        renderView = new AndroidFastRenderView(this, frameBuffer);
        graphics = new AndroidGraphics(getAssets(), frameBuffer);
        fileIO = new AndroidFileIO(getAssets());
        audio = new AndroidAudio(this);
        input = new AndroidInput(this, renderView, scaleX, scaleY);
        screen = getStartScreen();
        // Edit start 	Dao 	24/11/2012
        // setContentView(renderView);
        rootLayout = new RelativeLayout(this);
        keyboardView = new CustomKeyboardView(this, null);
        keyboardView.setVisibility(View.GONE);
        rootLayout.addView(renderView);
        rootLayout.addView(keyboardView);
        setContentView(rootLayout);
        // Edt end 		Dao 	24/12/2012
        
        PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
    }

    @Override
    public void onResume() {
        super.onResume();
        wakeLock.acquire();
        screen.resume();
        renderView.resume();
    }

    @Override
    public void onPause() {
        super.onPause();
        wakeLock.release();
        renderView.pause();
        screen.pause();

        if (isFinishing())
            screen.dispose();
    }

    @Override
    public Input getInput() {
        return input;
    }

    @Override
    public FileIO getFileIO() {
        return fileIO;
    }

    @Override
    public Graphics getGraphics() {
        return graphics;
    }

    @Override
    public Audio getAudio() {
        return audio;
    }

    @Override
    public void setScreen(Screen screen) {
        if (screen == null)
            throw new IllegalArgumentException("Screen must not be null");

        this.screen.pause();
        this.screen.dispose();
        screen.resume();
        screen.update(0);
        this.screen = screen;
    }
    
    public Screen getCurrentScreen() {
        return screen;
    }
    
    // Add start	Dao		24/11/2012
    
    public void showKeyboard(OnKeyboardActionListener listener) {
    	keyboardView.setOnKeyboardActionListener(listener);
    	renderView.showKeyboard();
    	showKeyboardWithAnimation();
    }
    
    public void hideKeyboard() {
    	keyboardView.setOnKeyboardActionListener(null);
    	keyboardView.setVisibility(View.GONE);
    	renderView.hideKeyboard();
    }
    
    private void showKeyboardWithAnimation() {
		if (keyboardView.getVisibility() == View.GONE) {
			Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_in_bottom);
			keyboardView.showWithAnimation(animation);
		}
	}
    // Add end		Dao		24/11/2012
}